//
//  ExhibitionViewController.m
//  MeiLiProductModel
//
//  Created by B505 on 15/2/21.
//  Copyright (c) 2015年 孙晓林. All rights reserved.
//

#import "ExhibitionViewController.h"
#import "DRNRealTimeBlurView.h"
#import "SliderViewController.h"
#import "ShowsDetailViewController.h"
#import "PublicMethod.h"
#import "T4Marco.h"
#import "UIButton+Utils.h"
#import "UILabel+Utils.h"

#define kCoverWidth 256
#define kCoverHeight 366
#define kYAxisHeight 106
#define kCollectBtnWidth 20
#define kShowsNameAwayFromCoverImageView 34
#define kShowNameLabelWidth 200
#define kShowNameLabelHeight 30
#define kStatusLabelWidth 120
#define kStatusLabelHeight 20
#define kRightBlurViewWidth 17
#define kLeftBlurViewWidth 237
#define kBlurViewHeight 58
#define kBlurViewYHeight 209
#define kLineWidth 1
#define kLineHeight 55
#define kEffectViewAlpha 0.25
#define kBottomImageHeight (SCREEN_HEIGHT - 637)

@interface ExhibitionViewController ()<UIScrollViewDelegate>
{
    NSArray *pictureArrray;
    NSArray *showsNameArray;
    NSArray *statusArray;
    NSInteger buttonTag;
}
@property (weak, nonatomic) IBOutlet UIImageView *blurImageView;
@property (weak, nonatomic) IBOutlet UIView *effectView;
@property (weak, nonatomic) IBOutlet UIScrollView *bkdScrollView;



@property (nonatomic, assign) BOOL isCollected;
@end

@implementation ExhibitionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _isCollected = YES;
    
    _effectView.backgroundColor = [UIColor blackColor];
    _effectView.alpha = kEffectViewAlpha;
    
    pictureArrray = @[@"pic0.jpg", @"pic1.jpg",@"pic2.jpg"];
    showsNameArray = @[@"中央美术学院美术馆", @"中国国家美术馆", @"中央美术学院"];
    statusArray = @[@"3个展览进行中", @"2个展览进行中", @"1个展览进行中"];
    [self designTheViewForShowsCovew];
    
}

- (void)designTheViewForShowsCovew
{
    CGFloat width = self.view.frame.size.width;
    CGFloat height = self.view.frame.size.height;
    UIImage *blurImage = [PublicMethod blurryImage:[PublicMethod cutImage:[UIImage imageNamed:pictureArrray[0]] WithImageViewWidth:width WithImageViewHeight:height] withBlurLevel:0.155];
    _blurImageView.image = blurImage;
    _bkdScrollView.bounces = YES;
    _bkdScrollView.showsHorizontalScrollIndicator = NO;
    _bkdScrollView.showsVerticalScrollIndicator = NO;
    _bkdScrollView.alwaysBounceVertical = YES;
    _bkdScrollView.pagingEnabled = YES;
    _bkdScrollView.delegate = self;
    for (int i = 0; i < pictureArrray.count; i ++) {
        
        UIImage *picImage = [PublicMethod cutImage:[UIImage imageNamed:pictureArrray[i]] WithImageViewWidth:kCoverWidth WithImageViewHeight:kCoverHeight];
        UIImageView *coverImageView = [[UIImageView alloc]initWithFrame:CGRectMake((SCREEN_WIDTH - kCoverWidth) / 2, kYAxisHeight + SCREEN_HEIGHT * i , kCoverWidth, kCoverHeight)];

        coverImageView.image = picImage;
        //shadow
        coverImageView.userInteractionEnabled=YES;//允许imageview接受touch
        coverImageView.layer.cornerRadius = 5.0;
        coverImageView.layer.shadowColor = [UIColor blackColor].CGColor;
        coverImageView.layer.shadowOpacity = 0.9;
        coverImageView.layer.shadowRadius = 8;
        coverImageView.layer.shadowOffset = CGSizeMake(0, -3);
        
        coverImageView.tag = 1000 + i;
        [_bkdScrollView addSubview:coverImageView];
        
        //btn
        UIButton *collectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        collectBtn.frame = CGRectMake(coverImageView.frame.origin.x + kCoverWidth - kCollectBtnWidth * 2.5 - 14, coverImageView.frame.origin.y + 11, kCollectBtnWidth, kCollectBtnWidth);
        [collectBtn setImageWithName:@"home_collect"];
        collectBtn.tag = 100 + i;
        [collectBtn addTarget:self action:@selector(onClickCollect:)];
        
        UIButton *infoBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        infoBtn.frame = CGRectMake(coverImageView.frame.origin.x + kCoverWidth - kCollectBtnWidth * 1.5, coverImageView.frame.origin.y + 11, kCollectBtnWidth, kCollectBtnWidth);
        [infoBtn setImageWithName:@"home_info"];
        infoBtn.tag = 200 + i;
        [infoBtn addTarget:self action:@selector(onClickInfo:)];
        [_bkdScrollView insertSubview:infoBtn aboveSubview:coverImageView];
        [_bkdScrollView insertSubview:collectBtn aboveSubview:coverImageView];
        
        //label
        UILabel *showsName = [UILabel labelWithFrame:CGRectMake((SCREEN_WIDTH - kShowNameLabelWidth) / 2, coverImageView.frame.origin.y + kCoverHeight + kShowsNameAwayFromCoverImageView, kShowNameLabelWidth, kShowNameLabelHeight)
                                                font:[UIFont systemFontOfSize:21]
                                       textAlignment:NSTextAlignmentCenter
                                           textColor:HEXCOLOR(0xFFFFFF)
                                highlightedTextColor:HEXCOLOR(0xFFFFFF)];
        showsName.text = showsNameArray[i];
        showsName.tag = 4000 + i;
        [_bkdScrollView addSubview:showsName];
        
        UILabel *statusLabel = [UILabel labelWithFrame:CGRectMake((SCREEN_WIDTH - kStatusLabelWidth) / 2, showsName.frame.origin.y + kShowNameLabelHeight + 13, kStatusLabelWidth, kShowNameLabelHeight)
                                                  font:[UIFont systemFontOfSize:13]
                                         textAlignment:NSTextAlignmentCenter
                                             textColor:HEXCOLOR(0xFFFFFF)
                                  highlightedTextColor:HEXCOLOR(0xFFFFFF)];
        statusLabel.alpha = 0.5;
        statusLabel.text = statusArray[i];
        statusLabel.tag = 5000 + i;
        [_bkdScrollView addSubview:statusLabel];
        
        //spearor Line
        
        UIButton *leftBlurview = [[UIButton alloc]initWithFrame:CGRectMake(coverImageView.frame.origin.x, coverImageView.frame.origin.y + kBlurViewYHeight, kLeftBlurViewWidth, kBlurViewHeight)];
        leftBlurview.alpha = 0.65;
        leftBlurview.backgroundColor = [UIColor lightGrayColor];
        leftBlurview.tag = 50 + i;
        [leftBlurview addTarget:self action:@selector(onClickEnlarge:)];
        UIImageView *lineImagView = [[UIImageView alloc]initWithFrame:CGRectMake(coverImageView.frame.origin.x + kLeftBlurViewWidth, coverImageView.frame.origin.y + kBlurViewYHeight + 5, kLineWidth, kLineHeight)];
        lineImagView.image = [UIImage imageNamed:@"home_line"];
        lineImagView.tag = 3000 + i;
        UIButton *rightBlurview = [[UIButton alloc]initWithFrame:CGRectMake(coverImageView.frame.origin.x + kLeftBlurViewWidth + 1, coverImageView.frame.origin.y + kBlurViewYHeight, kRightBlurViewWidth, kBlurViewHeight)];
        rightBlurview.alpha = 0.65;
        rightBlurview.tag = 2000 + i;
        rightBlurview.backgroundColor = [UIColor lightGrayColor];
        [_bkdScrollView addSubview:leftBlurview];
        [_bkdScrollView addSubview:lineImagView];
        [_bkdScrollView addSubview:rightBlurview];
        
        if (i < pictureArrray.count - 1) {
            
            UIImageView *bottomImageView = [[UIImageView alloc]initWithFrame:CGRectMake((SCREEN_WIDTH - kCoverWidth) / 2, statusLabel.frame.origin.y + kShowNameLabelHeight + 58 , kCoverWidth, kBottomImageHeight)];
            bottomImageView.tag = 500 + i;
            [_bkdScrollView addSubview:bottomImageView];
        }
    }
    
    UIImage *bottomImage = [PublicMethod cutImage:[UIImage imageNamed:pictureArrray[1]] WithImageViewWidth:kCoverWidth WithImageViewHeight:kBottomImageHeight];
    UIImageView *btmImageView = (UIImageView *)[_bkdScrollView viewWithTag:500];
    btmImageView.image = bottomImage;

}

- (void)onClickCollect:(UIButton *)btn
{
    T4_LOG_FUNC();
    [btn setSelected:_isCollected];
    _isCollected = !_isCollected;
    NSLog(@"%ld",btn.tag);
    
}

- (void)onClickInfo:(UIButton *)btn
{
    T4_LOG_FUNC();
    ShowsDetailViewController *detailView = [[ShowsDetailViewController alloc]init];
    [self presentViewController:detailView animated:YES completion:nil];
    
}

- (void)onClickEnlarge:(UIButton *)btn
{
    T4_LOG_FUNC();
    
    NSLog(@"%ld", btn.tag);
    
    //获得点击按钮的Tag值
    buttonTag = btn.tag;
    
    
    UIButton *leftBtn =(UIButton *)[_bkdScrollView viewWithTag:btn.tag];
    UIButton *rightBtn = (UIButton *)[_bkdScrollView viewWithTag:(btn.tag - 50 ) +2000];
    UIButton *collectBtn =(UIButton *)[_bkdScrollView viewWithTag:(btn.tag - 50 ) +100];
    UIButton *infoBtn =(UIButton *)[_bkdScrollView viewWithTag:(btn.tag - 50 ) +200];
    UIImageView *lineImageView = (UIImageView *)[_bkdScrollView viewWithTag:(buttonTag - 50 ) + 3000];
    UILabel *showsNameLabel = (UILabel *)[_bkdScrollView viewWithTag:(buttonTag - 50 ) + 4000];
    UILabel *statusLabel = (UILabel *)[_bkdScrollView viewWithTag:(buttonTag - 50 ) + 5000];
    UIImageView *bottomImageView = (UIImageView *)[_bkdScrollView viewWithTag:(buttonTag - 50 ) + 500];
    
    //真机由于计算速度快  没有产生相应的动画效果
    [UIView animateWithDuration:.8 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        for (int i = 0; i < 10; i ++) {
            
            [UIView animateWithDuration:.5 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
                
                CGRect leftBtnrect = leftBtn.frame;
                leftBtnrect.size.width -= 20;
                leftBtn.frame = leftBtnrect;
                
                CGRect rightBtnrect = rightBtn.frame;
                rightBtnrect.origin.x += kRightBlurViewWidth / 10;
                rightBtnrect.size.width -= kRightBlurViewWidth / 10;
                rightBtn.frame = rightBtnrect;
                
                [UIView animateWithDuration:0.2
                                 animations:^{
                                     collectBtn.alpha -= 0.1;
                                     infoBtn.alpha -= 0.1;
                                     lineImageView.alpha -= 0.1;
                                     showsNameLabel.alpha -= 0.1;
                                     statusLabel.alpha -= 0.1;
                                     bottomImageView.alpha -= 0.1;
                                     
                                 }
                                 completion:^(BOOL finished) {
                                     
                                 }];
                
                
                
            } completion:^(BOOL finished){
                leftBtn.hidden = YES;
                rightBtn.hidden = YES;
                
                
            }];
        }
        

        
    } completion:^(BOOL finished){
        
        
    }];

    
    [NSTimer scheduledTimerWithTimeInterval:0.8 target:self selector:@selector(startTransform) userInfo:nil repeats:NO];
    
    
}

- (void)startTransform
{
    UIImageView *coverImageView = (UIImageView *)[_bkdScrollView viewWithTag:(buttonTag - 50) + 1000];
    
    [UIView animateWithDuration:.8 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        
        coverImageView.transform=CGAffineTransformMakeScale(1.5, 1.8);
        
    } completion:^(BOOL finished){
        
        _bkdScrollView.hidden = YES;
        _effectView.hidden = YES;
        _blurImageView.image = [PublicMethod cutImage:[UIImage imageNamed:pictureArrray[buttonTag - 50]] WithImageViewWidth:SCREEN_WIDTH WithImageViewHeight:SCREEN_HEIGHT];
        
        //开始恢复
        [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(reFormTheView) userInfo:nil repeats:NO];
        
    }];

}

- (void)reFormTheView
{//恢复原状
    [UIView animateWithDuration:0.5
                     animations:^{
                     _blurImageView.image = [PublicMethod blurryImage:[PublicMethod cutImage:[UIImage imageNamed:pictureArrray[buttonTag - 50]] WithImageViewWidth:SCREEN_WIDTH WithImageViewHeight:SCREEN_HEIGHT] withBlurLevel:0.155];
                         
                     }
                     completion:^(BOOL finished) {
                         
                     }];
   
    
    UIImageView *coverImageView = (UIImageView *)[_bkdScrollView viewWithTag:(buttonTag - 50) + 1000];
    
    [UIView animateWithDuration:.8 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        
        coverImageView.transform=CGAffineTransformMakeScale(1.5, 1.8);
        
    } completion:^(BOOL finished){
        
        coverImageView.transform=CGAffineTransformMakeScale(1, 1);
        _bkdScrollView.hidden = NO;
        _effectView.hidden = NO;
        
        UIButton *leftBtn =(UIButton *)[_bkdScrollView viewWithTag:buttonTag];
        UIButton *rightBtn = (UIButton *)[_bkdScrollView viewWithTag:(buttonTag - 50 ) +2000];
        UIButton *collectBtn =(UIButton *)[_bkdScrollView viewWithTag:(buttonTag - 50 ) +100];
        UIButton *infoBtn =(UIButton *)[_bkdScrollView viewWithTag:(buttonTag - 50 ) +200];
        UIImageView *lineImageView = (UIImageView *)[_bkdScrollView viewWithTag:(buttonTag - 50 ) + 3000];
        UILabel *showsNameLabel = (UILabel *)[_bkdScrollView viewWithTag:(buttonTag - 50 ) + 4000];
        UILabel *statusLabel = (UILabel *)[_bkdScrollView viewWithTag:(buttonTag - 50 ) + 5000];
        UIImageView *bottomImageView = (UIImageView *)[_bkdScrollView viewWithTag:(buttonTag - 50 ) + 500];
        leftBtn.hidden = NO;
        rightBtn.hidden = NO;
        for (int i = 0; i < 10; i ++) {
            [UIView animateWithDuration:.5 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
                
                CGRect leftBtnrect = leftBtn.frame;
                leftBtnrect.size.width += 20;
                leftBtn.frame = leftBtnrect;
                
                CGRect rightBtnrect = rightBtn.frame;
                rightBtnrect.origin.x -= kRightBlurViewWidth / 10;
                rightBtnrect.size.width += kRightBlurViewWidth / 10;
                rightBtn.frame = rightBtnrect;
                
                [UIView animateWithDuration:0.2
                                 animations:^{
                                     lineImageView.alpha += 0.1;
                                     showsNameLabel.alpha += 0.1;
                                     statusLabel.alpha += 0.1;
                                     bottomImageView.alpha += 0.1;
                                     collectBtn.alpha += 0.1;
                                     infoBtn.alpha += 0.1;

                                 }
                                 completion:^(BOOL finished) {
                                     
                                 }];
                
            } completion:^(BOOL finished){
            }];
        }

    }];
    
    
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    _bkdScrollView.contentSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT * pictureArrray.count);
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    CGFloat yOffset = scrollView.contentOffset.y;
    int pageHeight = (int)self.view.frame.size.height;
    CGFloat changeAlpha = kEffectViewAlpha - (CGFloat)((int)yOffset % pageHeight) / pageHeight;
    CGFloat nextViewAlpha = (CGFloat)((int)yOffset % pageHeight) / pageHeight - kEffectViewAlpha;
    if (yOffset >= 0) {
        if (changeAlpha >= 0) {
            _effectView.alpha = changeAlpha;
        }else{
            if (nextViewAlpha <= 0.25) {
                _effectView.alpha = nextViewAlpha;
               
            }
            
        }
        
    }
    
    [UIView animateWithDuration:0.2
                     animations:^{
                         int index = yOffset / pageHeight;
                         CGFloat width = self.view.frame.size.width;
                         CGFloat height = self.view.frame.size.height;
                         UIImage *blurImage = [PublicMethod blurryImage:[PublicMethod cutImage:[UIImage imageNamed:pictureArrray[index]] WithImageViewWidth:width WithImageViewHeight:height] withBlurLevel:0.155];
                         _blurImageView.image = blurImage;
                         
                         if (index < pictureArrray.count - 1) {
                             UIImage *bottomImage = [PublicMethod cutImage:[UIImage imageNamed:pictureArrray[index + 1]] WithImageViewWidth:kCoverWidth WithImageViewHeight:kBottomImageHeight];
                             UIImageView *btmImageView = (UIImageView *)[_bkdScrollView viewWithTag:500 + index];
                             btmImageView.image = bottomImage;
                             
                             if ((int)yOffset % pageHeight) {
                                 btmImageView.hidden = YES;
                             }else{
                                 btmImageView.hidden = NO;
                             }
                         }
                     }
                     completion:^(BOOL finished) {
                        
                     }];
    
     NSLog(@"%f",yOffset);
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
